package com.moli.iov.base.util;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 内存分页
 *
 * @author whl
 * @date 2019-09-18 11:20:16
 */
public class PageUtils {
  /**
   *  * @currPageNo  页面传入的页号，从一开始  * @pageSize    每页记录数  
   */
  public static  <T> Map<String, Object>  getPagingResultMap(List<T> list, Integer currPageNo,
      Integer pageSize) {
    Map<String, Object> retMap = new HashMap<>();
    if (list.isEmpty()) {
      retMap.put("records", Collections.emptyList());
      retMap.put("pageNumber", 0);
      retMap.put("pageSize", 0);
      retMap.put("total", 0);
     // retMap.put("totalPageNum", 0);
      return retMap;
    }

    int totalRowNum = list.size();
    int totalPageNum = (totalRowNum - 1) / pageSize + 1;

    int realPageNo = currPageNo;
    if (currPageNo > totalPageNum) {
      realPageNo = totalPageNum;
    } else if (currPageNo < 1) {
      realPageNo = 1;
    }

    int fromIdx = (realPageNo - 1) * pageSize;
    int toIdx = realPageNo * pageSize > totalRowNum ? totalRowNum : realPageNo * pageSize;

    List<T> result = list.subList(fromIdx, toIdx);

    retMap.put("records", result);
    retMap.put("pageNumber", realPageNo);
    retMap.put("pageSize", result.size());
    retMap.put("total", totalRowNum);
    //retMap.put("totalPageNum", totalPageNum);

    return retMap;
  }

}
